package com.wushi.lenovo.asia5b.app_widget.span;

import android.text.Layout;
import android.text.Spannable;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.URLSpan;
import android.view.MotionEvent;
import android.widget.TextView;

import com.wushi.lenovo.asia5b.app_event.ReadNotificationEvent;
import com.wushi.lenovo.asia5b.app_model.inbox.InboxMsgModel;
import com.wushi.lenovo.asia5b.app_utils.helper.UriHelper;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import timber.log.Timber;

import static java.lang.String.format;
import static timber.log.Timber.d;

/**
 * Created by zhangyuncai on 2019/8/16.
 * 用于消息页面超链接
 */
public class InboxLink extends LinkMovementMethod {


    private final InboxMsgModel item;

    public InboxLink(InboxMsgModel item) {
        this.item = item;
    }

    /**
     * @param widget
     * @param buffer 点击 的TextView整个内容
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
        //默认跳转(默认点击整个ITEM)
        String skipUrl = "asia://www.asia5b.com/orderList?id=0";
        //如先设置默认跳转链接
        List<InboxMsgModel.Action> actionList = item.getAction();
        if (actionList != null && actionList.size() > 0) {
            for (InboxMsgModel.Action actionBean : actionList) {
                if (TextUtils.equals(actionBean.getRedirect(), "my order")) {
                    //成功匹配关键词
                    skipUrl = actionBean.getUrl();
                    break;
                }
            }
        }

        int action = event.getAction();
        if (action == MotionEvent.ACTION_UP) {

            try {


                int x = (int) event.getX();
                int y = (int) event.getY();

                x -= widget.getTotalPaddingLeft();
                y -= widget.getTotalPaddingTop();

                x += widget.getScrollX();
                y += widget.getScrollY();

                Layout layout = widget.getLayout();
                int line = layout.getLineForVertical(y);
                int off = layout.getOffsetForHorizontal(line, x);

                ClickableSpan[] links = buffer.getSpans(off, off, ClickableSpan.class);

                //点击的是超链接
                if (links.length != 0) {
                    ClickableSpan link = links[0];

                    int spanStart = buffer.getSpanStart(link);
                    int spanEnd = buffer.getSpanEnd(link);
                    CharSequence charSequence = buffer.subSequence(spanStart, spanEnd);
                    d(format("--->>>>>>>>---点击的关键词:" + charSequence));
                    URLSpan urlSpan = (URLSpan) link;
                    String urlSpanURL = urlSpan.getURL();
                    d("--->>>>>>>>---点击的超链接:" + urlSpanURL);
                    //如果已经符合跳转跳转要求,不需要往下执行
                    if (!TextUtils.isEmpty(urlSpanURL) && urlSpanURL.startsWith("asia5b:")) {
                        skipUrl = urlSpanURL;
                    }


                    //匹配关键词
                    if (actionList != null && actionList.size() > 0) {
                        for (InboxMsgModel.Action actionBean : actionList) {
                            if (TextUtils.equals(actionBean.getKeyword(), charSequence)) {
                                //成功匹配关键词
                                skipUrl = actionBean.getUrl();
                                break;
                            }
                        }
                    }


                    InboxMsgModel.ProductInfo extra_msg = item.getExtra_msg();
                    if (extra_msg != null && !TextUtils.isEmpty(skipUrl)) {
                        InboxMsgModel.ProductInfoBean product_info = extra_msg.getProduct_info();
                        if (product_info != null) {
                            String image = product_info.getImage();
                            if (TextUtils.equals(image, urlSpanURL) && !TextUtils.isEmpty(product_info.getLink())) {
                                //寻找商品详情的超链接
                                skipUrl = product_info.getLink();
                            }
                        }
                    }


                }

                if (!item.getCheck_read()) {
                    Timber.d("--->>>>>>>>---3/id:" + item.getId());
                    EventBus.getDefault().post(new ReadNotificationEvent(item.getId(), "order_notification"));
                }
                UriHelper.parseUri(skipUrl, widget.getContext());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public static InboxLink getInstance(InboxMsgModel item) {
        return new InboxLink(item);
    }
}
